/**
  ******************************************************************************
  * @file    :Main-legacy.c
  * @author  :MG Team
  * @version :V1.0
  * @date
  * @brief   :for legacy adv 
  ******************************************************************************
***/

/* Includes ------------------------------------------------------------------*/
#include "Includes.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern unsigned short tick;
extern uint8_t adv_data[];

/* Private function prototypes -----------------------------------------------*/
void BLE_RESET(void)
{
    GPIO_ResetBits(BLE_nRST_PORT,BLE_nRST);
    Delay_ms(2);
    GPIO_SetBits(BLE_nRST_PORT,BLE_nRST);
    Delay_ms(20);
#if (COMM_OPT_SEL == COMM_I2C_SIMU_NOPU)
    GPIO_Init(BLE_SCL_PORT,BLE_SCL,GPIO_Mode_Out_PP_High_Slow);
#endif
}

/*******************************************************************************
* Function	 :		main
* Parameter  :		void
* Returns	 :		void
* Description:
* Note: 	 :
*******************************************************************************/
void main(void)
{
    static uint8_t flag_pressed = 0;

    //init mcu system
    Init_System();

    LED_RED_ON();
    LED_GREEN_ON();
    Delay_ms(20);

#if (POWER_SAVE_OPT == POWER_SAVE_STANDBY)
    BLE_RESET();
#endif

    ////BLE initial
    BLE_Init();
    Uart_Send_String("BLE init ok.\r\n");

# if 0 //carrier test
    SRCC_CarrierTest(19); //0:2402, 19:2440, 39:2480
    Uart_Send_String("carrier testing 2440...");

    while(1)
    {
        if(KEY_GET() == RESET)
        {
            LED_RED_OFF();
           //push button to exit carrier mode
            MG_Write_Reg(BASEBAND_CFG, 2); //STOP=2
            break;
        }
        else
        {
            LED_RED_ON();
            flag_pressed = 0;
        }
    }

    BLE_Init();
    Uart_Send_String("BLE init ok..\r\n");
#endif

    //////ble tx api
    BLE_TX();

    tick = 5000; //tx 3s
    while(1)
    {
#if 1   //////user proc - power save demo
        if(2000 == tick) //work time: (5000-2000)ms,  pause time: 2000 ms
        {
#if (POWER_SAVE_OPT == POWER_SAVE_SLEEP)
            BLE_Mode_Sleep();
#elif (POWER_SAVE_OPT == POWER_SAVE_STANDBY)
            BLE_Mode_Stdby();
    #if (COMM_OPT_SEL == COMM_I2C_SIMU_NOPU)
            GPIO_Init(BLE_SCL_PORT,BLE_SCL,GPIO_Mode_Out_OD_HiZ_Slow);
            GPIO_Init(BLE_SDA_PORT,BLE_SDA,GPIO_Mode_Out_OD_HiZ_Slow);
    #endif
#endif
            LED_GREEN_OFF();
            Uart_Send_String("BLE tx done.\r\n");
            Uart_Send_Byte(POWER_SAVE_OPT);
        }
        else if(0 == tick)
        {
            tick = 5000;
#if (POWER_SAVE_OPT == POWER_SAVE_STANDBY)
            BLE_RESET();//trigger nRST
            BLE_Init();
#endif
            BLE_TX();
            LED_GREEN_ON();
            Uart_Send_String("BLE tx start.\r\n");
        }
#endif
        //key test
        if(KEY_GET() == RESET)
        {
            if(flag_pressed == 0){
                flag_pressed = 1;
                MG_Write_Reg(BASEBAND_CFG, 6); //SLP_XO=4, STOP=2
                adv_data[22]++; 
                BLE_TX(); 
            }
            LED_RED_OFF();

        }else {
            flag_pressed = 0;
            LED_RED_ON();

        }
    }
}
